<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>
    <script type="text/javascript" src="/socket.io/socket.io.js"></script>
  </head>
  <body>
    <% if(user){ %>
    <nav>
        <h3><%=user.loginname%> 已登陆</h3>
        <a href="/users/logout">退出</a>
    </nav>
    <br>
      <input type="text" name="" onchange="say(event)" value="">
      <input type="submit" name="" value="提交">
    <br><br>
    <div class="list">

    </div>
    <script type="text/javascript">
      alert("登录成功");
      const socket = io.connect();
      const listdom = document.querySelector(".list");

      socket.emit("join",alert('登录成功'));

      socket.on("announcement",function(msg){
        var li = document.createElement("li");
        li.innerHTML = msg;
        listdom.appendChild(li);
      });

      socket.on("newsay",word=>{
          var li = document.createElement("li");
          li.innerText = word;
          listdom.appendChild(li);
      });
      function say(event){
        socket.emit("say",event.target.value);
      }
      // socket.on("welcome",data=>{//测试代码
      //   console.log(data);
      //   socket.emit("question"," 我要一杯美式 ");
      // });
    </script>

    <%}else{%>
      <h3>登陆</h3>
    <form class="" action="/users/login" method="post">
      <input type="text" name="loginname" placeholder="用户名" value="">
      <br>
      <input type="password" name="password" placeholder="密码" value="">
      <br>
      <%= firstnum %> +<%= secondnum %> = <input type="text" name="vnum">
      <br>
      <input type="submit" name="" value="登录">
    </form>
    <br><br>
    <a href="/users/reg">注册</a>
    <%}%>
  </body>
</html>
